function ismobile() {

    var browser = {
        versions: function () {
            var u = navigator.userAgent, app = navigator.appVersion;
            return {
                trident: u.indexOf('Trident') > -1, //IE�ں�
                presto: u.indexOf('Presto') > -1, //opera�ں�
                webKit: u.indexOf('AppleWebKit') > -1, //ƻ�����ȸ��ں�
                gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,//����ں�
                mobile: !!u.match(/AppleWebKit.*Mobile.*/), //�Ƿ�Ϊ�ƶ��ն�
                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios�ն�
                android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, //android�ն�
                iPhone: u.indexOf('iPhone') > -1, //�Ƿ�ΪiPhone����QQHD�����
                iPad: u.indexOf('iPad') > -1, //�Ƿ�iPad
                webApp: u.indexOf('Safari') == -1, //�Ƿ�webӦ�ó���û��ͷ����ײ�
                weixin: u.indexOf('MicroMessenger') > -1, //�Ƿ�΢�� ��2015-01-22������
                qq: u.match(/\sQQ/i) == " qq" //�Ƿ�QQ
            };
        }(),
        language: (navigator.browserLanguage || navigator.language).toLowerCase()
    };

    if (browser.versions.mobile == true) {
        if (browser.versions.iPhone == true) {
            return '1';
        } else if (browser.versions.android == true) {
            return '0';
        }
    } else {

    }
}

var isMobile = ismobile();

var obj, token, uid, addressID, startDate, endDate;

document.onreadystatechange = function () {

    if (document.readyState == "complete") {

        if (isMobile == 0) {

            window.android.getAndroidId("pp")

        }
    }

};
function WebViewDidFinishLoad() {

    var message = {

        "VcWay": "SendId"
    };

    window.webkit.messageHandlers.CallSiteModel.postMessage(message);
}

//getNav(600, 13, "030BAFAB-D049-FE0D-3490-5EB3DEDC27D3");


var belong_model_area = 600;

var curNavIndex = 0;

var mescrollrr = [];

var paramsArr = [];


function formId() {

    if (isMobile == 1) {

        CallSiteModel.jsCallOCWithSendId()

    } else if (isMobile == 0) {

        window.android.getAndroidId("pp");
    }
}


function receive_android_device_location_search(k) {

    //console.log(k)

    obj = $.parseJSON(k);

    token = obj.TOKEN;

    uid = obj.uid;

    addressID = obj.districtId;

    startDate = timeFormat(obj.startsDate);

    endDate = timeFormat(obj.endDate);

    getNav(belong_model_area, uid, token)
}

function receiveiOSDateAndAddressData(k) {

    obj = $.parseJSON(k);
    
    //console.log(obj)

    token = obj.TOKEN;

    uid = obj.uid;

    addressID = obj.districtId;

    startDate = timeFormat(obj.startsDate);

    endDate = timeFormat(obj.endDate);

    getNav(belong_model_area, uid, token)
}

function getNav(belong_model_area, uid, token) {

    $.ajax({

        type: "POST",

        url: url + "users/navigationbar",

        dataType: "json",

        data: {

            "belong_model": belong_model_area
        },

        beforeSend: function (xhr) {

            xhr.setRequestHeader("TOKEN", token)

        },

        success: function (data) {

            //console.log(data);

            if (data.codes == 200) {

                var html = template("navlistTpl", data);

                $("header #ul").html(html);

                var html1 = template("ListTpl", {data: data.data.list});

                $("#content").html(html1);

                curNavIndex = 0;

                mescrollrr = new Array(data.data.list.length + 1);

                mescrollrr[0] = initMescroll("mescroll0", "dataList0");

                paramsArr = new Array(data.data.list.length + 1);

                paramsArr[0] = initParams(0);

                navClick();
            }


        },
        error: function (info) {

            console.log(info)
        }
    });
}

function Params(page, size, cat_id, belong_model, price_start, price_end, hot_sort, price_sort, address, cat_level, start_time, end_time, url, keywords, goods_sn) {
    this.page = page;

    this.size = size;

    this.cat_id = cat_id;

    this.belong_model = belong_model;

    this.price_start = price_start;

    this.price_end = price_end;

    this.hot_sort = hot_sort;

    this.price_sort = price_sort;

    this.address_id = address;

    this.start_time = start_time;

    this.end_time = end_time;

    this.url = url;

    this.keywords = keywords;

    this.cat_level = cat_level;

    this.goods_sn = goods_sn;
}

function initParams(cat_id) {

    var params = new Params(1, 10, cat_id, belong_model_area);

    params.cat_level = 100;

    params.hot_sort = null;

    params.price_sort = null;

    params.price_start = null;

    params.price_end = null;

    params.address_id = addressID;

    params.start_time = startDate;

    params.end_time = endDate;

    params.url = "product/goods/webList";

    return params;
}

function initMescroll(mescrollId, clearEmptyId) {

    var mescroll = new MeScroll(mescrollId, {

        up: {

            callback: upCallback,

            noMoreSize: 5,

            isBounce: false,

            clearEmptyId: clearEmptyId,

            empty: {

                icon: "../../person_home/img/wallet_img_no@3x.png",

                tip: "暂无相关数据~ 再逛逛~"
            }
        }
    });
    return mescroll;
}

function upCallback(page) {


//console.log("llll")

    var dataIndex = curNavIndex;

    var params = paramsArr[dataIndex];

    params.page = page.num;

    params.size = page.size;

    //console.log(page)


    getListDataFromNet(dataIndex, params, function (data) {

        if (data.codes == 200) {

            var listData = data.data.list;

            mescrollrr[dataIndex].endBySize(listData.length, data.totalCount);

            listData.forEach(function (item) {

                if (item.goods_tag) {

                    if (item.goods_tag.length > 3) {

                        item.goods_tag.length = 3;
                    }
                }
            });

            setListData(data, dataIndex);


        } else {

            mescrollrr[dataIndex].endBySize(0, 0);

            mescrollrr[dataIndex].showEmpty();

            //$(".team").hide()


        }
    }, function () {

        mescrollrr[dataIndex].endErr();


    });
}

function setListData(data, dataIndex) {

    var dataList = "dataList" + dataIndex;

    var html = template("datalistTpl", data);

    $("#" + dataList).append(html);

    if (isiphoneX) {

        $(".footer_position").addClass("footer");

        $(".mescroll").css("padding-bottom", "0.484rem")

    } else {

        $(".footer_position").removeClass("footer")
    }


}

function getListDataFromNet(dataIndex, params, successCallback, errorCallback) {

    //console.log(params);

    $.ajax({

        url: url + 'users/fourModel',

        type: "POST",

        dataType: "json",

        data: params,

        success: function (data) {

            //console.log(data)

            //alert(JSON.stringify(data))

            successCallback(data);
        },

        error: errorCallback

    });
}

function navClick() {

    $("#ul").on("click", "li", function () {

        var index = Number($(this).data("index"));

        if (curNavIndex != index) {

            $("#mescroll" + curNavIndex).hide().addClass("hide");

            mescrollrr[curNavIndex].hideTopBtn();


            $("#mescroll" + index).show().removeClass("hide");


            if (paramsArr[index] == null || mescrollrr[index] == null) {

                paramsArr[index] = initParams($(this).attr("id"));

                mescrollrr[index] = initMescroll("mescroll" + index, "dataList" + index);

            } else {


                var curMescroll = mescrollrr[index];

                var curScrollTop = curMescroll.getScrollTop();

                if (curScrollTop >= curMescroll.optUp.toTop.offset) {

                    curMescroll.showTopBtn();

                } else {

                    curMescroll.hideTopBtn();
                }
            }

            resetFilter($(this), curNavIndex);

            curNavIndex = index;
        }
    });
}

var isTrue = true;
$("#content").on("click", "li", function () {

    if (isTrue) {

        var id = $(this).attr("id");

        if(!id) return false;

        var json = {

            "title": '场地详情',

            "url": 'https://viewapi.aipics.cn/view_h5/four_plates/area/areaDetail.html',

            //"site_id": id,

            "TOKEN": token,

            "addressID": addressID,

            "startDate": startDate,

            "endDate": endDate,

            "goods_id": id,

            "uid": uid


        };

        //alert(JSON.stringify(json));

        if (isMobile == 1) {

            var message = {

                "VcData": json,

                "VcName": "SiteDetails",

                "VcWay": "PushVCName"
            };

            window.webkit.messageHandlers.CallSiteModel.postMessage(message);

            //CallSiteModel.jsCalliOSWithPushVCName({
            //
            //    "VcData": json,
            //
            //    "VcName": "SiteDetails"
            //})
        } else if (isMobile == 0) {

            window.android.jumpToProjectFielsSpaceDetail(JSON.stringify(json))
        }
        isTrue = false;
    }
    setTimeout(function () {

        isTrue = true;

    }, 500);

});

//重置筛选条件
function resetFilter(that, oldIndex) {

    that.parents("header").siblings("div.sort").find(".sortWay p").html("排序方式").removeClass("sortColor").siblings("b").removeClass("sortBackImg1");

    that.parents("header").siblings("div.sort").find(".priceRange p").html("价格区间").removeClass("sortColor").siblings("b").removeClass("sortBackImg1");

    that.parents("header").siblings("div.sort").find(".sdrop_down li").removeClass("sortColor").find(".ok").addClass("hide");

    that.parents("header").siblings("div.sort").find(".pdrop_down li").removeClass("sortColor").find(".ok").addClass("hide");

    var params = paramsArr[oldIndex];

    params.hot_sort = 0;

    params.price_sort = 0;

    params.price_start = 0;

    params.price_end = 0;
}

//格式化时间
function timeFormat(time) {//2017-12-27 12:00

    var y = time.substring(0, 4);

    var m = time.substring(5, 7);

    var d = time.substring(8, 10);

    var h = time.substring(11, 13);

    var s = time.substring(14, 16);

    // return(s)

    return (y + m + d + h + s);
}

$("header").on("click", "ul>li>a", function () {

    $("header>ul>li>a").removeClass("active").css("text-decoration", "none");

    $(this).addClass("active").css("text-decoration", "none");
});

//排序方式
$(".sortWay").on("click", function () {

    $(".sortWay p").addClass("sortColor");

    $(".sortWay b").addClass("sortBackImg");

    if ($(".sdrop_down").is(':hidden')) {

        $(".overlay").show();

        $(".sdrop_down").show();

        $(".pdrop_down").hide();

    } else {

        $(".overlay").hide();

        $(".sdrop_down").hide();
    }
});
//价格区间
$(".priceRange").on("click", function () {

    $(".priceRange p").addClass("sortColor");

    $(".priceRange b").addClass("sortBackImg");


    if ($(".pdrop_down").is(':hidden')) {

        $(".overlay").show();

        $(".pdrop_down").show();

        $(".sdrop_down").hide();

    } else {

        $(".overlay").hide();

        $(".pdrop_down").hide();
    }
});
//排序下拉ul里li
$(".sortBox").on("click", ".sdrop_down>li", function () {

    $(".sdrop_down>li").removeClass("sortColor").find(".ok").addClass("hide");

    $(this).addClass("sortColor");

    $(this).find(".ok").removeClass("hide");

    $(".overlay").hide();

    $(".sdrop_down").hide();

    var content = $(this).find("span").eq(0).html();

    $(".sortWay>p").html(content);

    $(".sortWay>b").removeClass("sortBackImg").addClass("sortBackImg1")

});
//价格区间下拉ul里li
$(".sortBox").on("click", ".pdrop_down>li", function () {

    $(".pdrop_down>li").removeClass("sortColor").find(".ok").addClass("hide");

    $(this).addClass("sortColor");

    $(this).find(".ok").removeClass("hide");

    $(".overlay").hide();

    $(".pdrop_down").hide();

    var content = $(this).find("span").eq(0).html();

    $(".priceRange>p").html(content);

    $(".priceRange>b").removeClass("sortBackImg").addClass("sortBackImg1")
});